﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GUILayerWPF.FlightRoutingService;
using System.Drawing;

namespace GUILayerWPF
{
    /// <summary>
    /// Interaction logic for createCustomer.xaml
    /// </summary>
    public partial class createCustomer : Window
    {
        //Variabler initialization
        private static FlightRouteServiceClient flightclient;
        private Customer retPerson;
        public createCustomer()
        {
            InitializeComponent();
            flightclient = new FlightRouteServiceClient();
            tickPic.Visibility = System.Windows.Visibility.Collapsed;
            redCross.Visibility = System.Windows.Visibility.Collapsed;
            retPerson = new Customer();
        }
        //Logon button click
        private void loginBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string inputPhone = searchBox.Text;
                searchBox.BorderThickness = new Thickness(1);
                retPerson = flightclient.FindCustomerByPhoneNumber(System.Convert.ToInt32(inputPhone));
                if (retPerson != null)
                {
                    redCross.Visibility = System.Windows.Visibility.Hidden;
                    tickPic.Visibility = System.Windows.Visibility.Visible;
                    FNameBox.Text = retPerson.FName;
                    LNameBox.Text = retPerson.LName;
                    AdressBox.Text = retPerson.Address;
                    zipCodeBox.Text = System.Convert.ToString(retPerson.ZipCode);
                    PhoneNoBox.Text = System.Convert.ToString(retPerson.PhoneNo);
                    PassportNoBox.Text = retPerson.PassportNo;

                    
                    //åbne booking GUI 
                    CreateBooking createBooking = new CreateBooking(retPerson);
                    createBooking.Show();
                    Application.Current.MainWindow.Close();
                }
                if (retPerson == null)
                {
                    searchBox.Text = "TelefonNummeret findes ikke i systemet";
                    tickPic.Visibility = System.Windows.Visibility.Hidden;
                    redCross.Visibility = System.Windows.Visibility.Visible;
                }
                else if(searchBox.Text.Length < 8)
                {
                    searchBox.Text = "Ikke nok tal angivet";
                    searchBox.BorderThickness = new Thickness(2);
                    searchBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
                }
                else if(searchBox.Text.Length > 8)
                {
                    searchBox.Text = "For mange tal angivet";
                    searchBox.BorderThickness = new Thickness(2);
                    searchBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
                }
            }
            catch
            {
                searchBox.Text = "Teksten er ikke valid";
                searchBox.BorderThickness = new Thickness(2);
                searchBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
            }
        }

        //button create click
        private void ButtonCreate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                retPerson.FName = FNameBox.Text;
                FNameBox.BorderThickness = new Thickness(1);
                FNameBox.SetValue(Border.BorderBrushProperty, Brushes.Green);
            }
            catch
            {
                FNameBox.Text = "Teksten er ikke valid";
                FNameBox.BorderThickness = new Thickness(2);
                FNameBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
            }
            try
            {
                retPerson.LName = LNameBox.Text;
                LNameBox.BorderThickness = new Thickness(2);
                LNameBox.SetValue(Border.BorderBrushProperty, Brushes.Green);
            }
            catch
            {
                LNameBox.Text = "Teksten er ikke valid";
                LNameBox.BorderThickness = new Thickness(2);
                LNameBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
            }
            try
            {
                retPerson.Address = AdressBox.Text;
                LNameBox.BorderThickness = new Thickness(2);
                LNameBox.SetValue(Border.BorderBrushProperty, Brushes.Green);
            }
            catch
            {
                AdressBox.Text = "Teksten er ikke valid";
                AdressBox.BorderThickness = new Thickness(2);
                AdressBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
            }
            try
            {
                if(zipCodeBox.Text.Length == 4)
                { 
                retPerson.ZipCode = System.Convert.ToInt32(zipCodeBox.Text);
                zipCodeBox.BorderThickness = new Thickness(2);
                zipCodeBox.SetValue(Border.BorderBrushProperty, Brushes.Green);
                }
                else
                {
                    zipCodeBox.Text = "Postnummer findes ikke";
                    zipCodeBox.BorderThickness = new Thickness(2);
                    zipCodeBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
                }
            }
            catch
            {
                zipCodeBox.Text = "Teksten er ikke valid";
                zipCodeBox.BorderThickness = new Thickness(2);
                zipCodeBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
            }
            try
            {
                if (PhoneNoBox.Text.Length == 8)
                {
                    retPerson.PhoneNo = System.Convert.ToInt32(PhoneNoBox.Text);
                    PhoneNoBox.BorderThickness = new Thickness(2);
                    PhoneNoBox.SetValue(Border.BorderBrushProperty, Brushes.Green);
                }
                else
                {
                    PhoneNoBox.Text = "Teksten er ikke valid";
                    PhoneNoBox.BorderThickness = new Thickness(2);
                    PhoneNoBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
                }
            }
            catch
            {
                PhoneNoBox.Text = "Teksten er ikke valid";
                PhoneNoBox.BorderThickness = new Thickness(2);
                PhoneNoBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
            }
            try
            {
                retPerson.PassportNo = PassportNoBox.Text;
                PassportNoBox.BorderThickness = new Thickness(2);
                PassportNoBox.SetValue(Border.BorderBrushProperty, Brushes.Green);
            }
            catch
            {
                PassportNoBox.Text = "Teksten er ikke valid";
                PassportNoBox.BorderThickness = new Thickness(2);
                PassportNoBox.SetValue(Border.BorderBrushProperty, Brushes.Red);
            }
            
            string retString = "Denmark";
            retPerson.Country = retString;
            int i = flightclient.InsertCustomer(retPerson);

            if(i > 0)
            { 
            CreateBooking createBooking = new CreateBooking(retPerson);
            createBooking.Show();
            Application.Current.MainWindow.Close();
            }
            else
            {
                MessageBox.Show("Personen blev ikke oprettet tjek om de indtastet værdier er korrekte og prøv igen");
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FNameBox.Clear();
            LNameBox.Clear();
            AdressBox.Clear();
            zipCodeBox.Clear();
            PhoneNoBox.Clear();
            PassportNoBox.Clear();
        }

    }
}
